package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.Settlement;
import com.ruoyi.business.mapper.SettlementMapper;
import com.ruoyi.business.service.SettlementService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 结算Service业务层处理
 * 
 * @author xx
 * @date 2020-04-03
 */
@Service
public class SettlementServiceImpl implements SettlementService {
    @Autowired
    private SettlementMapper settlementMapper;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询结算
     * 
     * @param id 结算ID
     * @return 结算
     */
    @Override
    public Settlement selectSettlementById(Long id) {
        return settlementMapper.selectSettlementById(id);
    }

    /**
     * 查询结算列表
     * 
     * @param settlement 结算
     * @return 结算
     */
    @Override
    public List<Settlement> selectSettlementList(Settlement settlement) {
        settlement.setBusinessType(0);
        SysUser sysUser = new SysUser();
        if (Objects.nonNull(settlement.getUserId())) {
            sysUser.setPid(settlement.getUserId());
            settlement.setUserIds(sysUserService.selectUserList(sysUser).stream().map(SysUser::getUserId).collect(Collectors.toList()));
        }
        return settlementMapper.selectSettlementList(settlement);
    }

    /**
     * 新增结算
     * 
     * @param settlement 结算
     * @return 结果
     */
    @Override
    public int insertSettlement(Settlement settlement) {
        settlement.setCreateTime(DateUtils.getNowDate());
        return settlementMapper.insertSettlement(settlement);
    }

    /**
     * 修改结算
     * 
     * @param settlement 结算
     * @return 结果
     */
    @Override
    public int updateSettlement(Settlement settlement) {
        return settlementMapper.updateSettlement(settlement);
    }

    /**
     * 删除结算对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSettlementByIds(String ids) {
        return settlementMapper.deleteSettlementByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除结算信息
     * 
     * @param id 结算ID
     * @return 结果
     */
    @Override
    public int deleteSettlementById(Long id) {
        return settlementMapper.deleteSettlementById(id);
    }
}
